package com.boot2.core.exception;

import com.boot2.core.web.RequestContext;
import lombok.extern.apachecommons.CommonsLog;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangweilin
 * @ClassName: BusinessException
 * @description: 业务异常
 * @date 2017年12月19日 下午4:54:48
 */
@CommonsLog
public class BusinessException extends RuntimeException {
    /**
     *
     */
    private static final long serialVersionUID = -3877531184268150162L;

    public BusinessException(Throwable e) {
        super(e);
    }

    public BusinessException(Throwable e, boolean isLog) {
        super(e);
        if (isLog) {
            HttpServletRequest request = RequestContext.getRequest();
            if (null==request) {
                log.error(String.format(e.getMessage()));
            }
        }
    }

    public BusinessException(String message, String... paras) {
        super(String.format(message, paras));
    }

    public BusinessException(String message, boolean isLog, String... paras) {
        super(String.format(message, paras));
        if (isLog) {
            HttpServletRequest request = RequestContext.getRequest();
            if (null==request) {
                log.error(String.format(message, paras));
            }
        }
    }

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(String message, boolean isLog) {
        super(message);
        if (isLog) {
            HttpServletRequest request = RequestContext.getRequest();
            if (null==request) {
                log.error(message);
            }
        }
    }

    public BusinessException(String message, Throwable e, String... paras) {
        super(String.format(message, paras), e);
    }

    public BusinessException(String message, Throwable e, boolean isLog, String... paras) {
        super(String.format(message, paras), e);
        if (isLog) {
            HttpServletRequest request = RequestContext.getRequest();
            if (null==request) {
                log.error(String.format(message, paras), e);
            }
        }
    }

    public BusinessException(String message, Throwable e) {
        super(message, e);
    }

    public BusinessException(String message, Throwable e, boolean isLog) {
        super(message, e);
        if (isLog) {
            HttpServletRequest request = RequestContext.getRequest();
            if (null==request) {
                log.error(message, e);
            }
        }
    }

}
